package com.qfedu.controller;

import com.qfedu.model.Project;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.IProjectService;
import com.qfedu.service.impl.ProjectServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/6/27 12:48
 */

@RestController
@RequestMapping("project")
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    @GetMapping("findAll")
    public List<Project> findAll() {
        return projectService.findAll();
    }

    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPage) {
        PageResult page = projectService.findPage(queryPage);
        return page;
    }

    @PostMapping("add")
    public Result addProject(@RequestBody Project project) {
        int row = projectService.addProject(project);
        if (row > 0) {
            //枚举类名.对象名.get方法 获取到对应的属性值
            //Status.SUCCESS.name();  获取到枚举对象名  SUCCESS
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    @GetMapping("findByPid")
    public Result findByPid(@RequestParam("pid") int pid) {
        Project project = projectService.findByPid(pid);
        Result result = new Result();
        result.setMessage("file");
        if (ObjectUtils.isNotEmpty(project)) {
            result.setFlag(true);
            result.setData(project);
            result.setMessage("success");
        }
        return result;
    }


    @PostMapping("edit")
    public Result edit(@RequestBody Project project) {
        int row = projectService.edit(project);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }



    /**
     * @Author Tbh.
     * @Description: 逻辑删除
     * @Date 2022/6/30 15:21
     * @Param 工程id
     * @return 删除结果
     */
    @GetMapping("delete")
    public Result edit(@RequestParam("pid") Integer pid) {
        int row = projectService.deleteByPid(pid);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }
}
